﻿using Serilog;
using Serilog.Events;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Helpers
{
    public class SerilogHelperOptions
    {
        public const string SectionName = "SerilogHelper";
        public const string WindowsAppLogPath = "WindowsAppLogPath";
        /// <summary>
        /// exceptionless用户名称
        /// </summary>
        public string UserIdentity { get; set; }
        /// <summary>
        /// 文件输出的默认模板
        /// </summary>
        public string FileOutputTemplate { get; set; } = "{Timestamp:HH:mm:ss} {Level:u3} {userName}{name}{user} 【{Message:lj}】 {SourceContext:l}{NewLine}{Exception}";
        /// <summary>
        /// 控制台输出的默认模板
        /// </summary>
        public string ConsoleOutputTemplate { get; set; } = "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}";
        /// <summary>
        /// 日志路径,默认选择最后一个盘
        /// 以iis或者window服务启动 发现不了Z盘(挂载的nas盘 协议smb)
        /// </summary>
        public string LogPath { get; set; }
        /// <summary>
        /// windows系统日志保存路径,为空的话自动创建路径
        /// </summary>
        public string WindowsLogPath { get; set; }
        /// <summary>
        /// 最大保留的文件数量
        /// </summary>
        public int? RetainedFileCountLimit { get; set; } = null;
        /// <summary>
        /// 最长保留时间
        /// </summary>
        public TimeSpan? RetainedFileTimeLimit { get; set; } = TimeSpan.FromDays(3);
        /// <summary>
        /// 日志的最低输出等级
        /// </summary>
        public LogEventLevel MinimumLevel { get; set; } = LogEventLevel.Information;
        /// <summary>
        /// 是否记录到控制台
        /// </summary>
        public bool IsLogToConsole { get; set; } = false;
        /// <summary>
        /// 是否记录到本地文件
        /// </summary>
        public bool IsLogToFile { get; set; } = true;
        /// <summary>
        /// 是否捕获全局异常
        /// </summary>
        public bool IsLogUnhandledException { get; set; } = true;
        public UnhandledExceptionEventHandler UnhandledException { get; set; } = (s, e) =>
        {
            try
            {
                Exception ex = e.ExceptionObject as Exception;
                Log.Fatal(ex, "UnhandledException");
            }
            catch (Exception)
            {
            }
        };

        public EventHandler<UnobservedTaskExceptionEventArgs> UnobservedTaskException { get; set; } = (s, e) =>
        {
            try
            {
                Log.Fatal(e.Exception.GetBaseException(), "UnobservedTaskException");
                e.SetObserved();
            }
            catch (Exception)
            {
            }
        };
        /// <summary>
        /// 环境名称
        /// </summary>
        internal string EnvName { get; set; }

        /// <summary>
        /// 应用名称,在windows系统下已此命名文件夹
        /// </summary>
        public string AppName { get; set; }
        /// <summary>
        /// 应用类别,在自动创建日志路径后,再增加次名字的子文件夹
        /// </summary>
        public string AppMode { get; set; }
        /// <summary>
        /// 默认从环境变量computername获取,docker从HOST_Q和HOSTNAME获取
        /// </summary>
        public string ComputerName { get; set; } = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ?
            (Environment.GetEnvironmentVariable("computername", EnvironmentVariableTarget.User) ?? Environment.GetEnvironmentVariable("computername", EnvironmentVariableTarget.Machine) ?? Environment.GetEnvironmentVariable("computername"))
            : (Environment.GetEnvironmentVariable("HOST_Q") ?? Environment.GetEnvironmentVariable("HOSTNAME"));
    }
}
